//
//  AppDelegate.swift
//  weibo
//
//  Created by cmh on 15/8/31.
//  Copyright © 2015年 cmh. All rights reserved.
//

import UIKit
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // 注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootViewContriller:", name: SwitchRootViewContrillerNotification, object: nil)
        
        // 一开始就设置好文字的颜色
        setTitleColor()
        
        setNetwork()
        
        // 设置根视图
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()

        window?.rootViewController = showRootViewController()
        
        window?.makeKeyAndVisible()
        
        return true
    }
    
    // 监听通知的事件
    func switchRootViewContriller(notification: NSNotification) {
        
        // 根据类型，切换不同的控制器
        window?.rootViewController = (notification.object == nil) ? MainTabBarController() : WelcomeViewController()
    
    }
    
    // 注销指定名称的通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: SwitchRootViewContrillerNotification, object: nil)
    }
    
    // 是否更新新版本
    private func isNewVersion() -> Bool {
    
        // 当前版本号
        let bundleVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        
//        printLog("当前的版本：\(bundleVersion)")
        
        // 之前的版本
        let versionKey = "weibo.version"
        
        let beforeVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        
        // 保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(bundleVersion, forKey: versionKey)
        
        return bundleVersion > beforeVersion
    }
    
    // 判断用户是否登陆，及版本号，显示对应的界面
    private func showRootViewController() -> UIViewController {
        
        // 登陆的话依据版本号，显示对应的界面
        if UserAccountViewModel.shareUserAccount.userLogon {
        
            return  isNewVersion() ? NewFeatureViewController() : WelcomeViewController()
            
        }
        
        // 没有登陆就显示登陆界面
        return MainTabBarController()
    }

    // 设置tab/nav文字的颜色
    private func setTitleColor() {
        
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        // 不透明
        UINavigationBar.appearance().translucent = false

        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        // 不透明
        UINavigationBar.appearance().translucent = false
    }
    
    // 设置网络指示器
    private func setNetwork() {
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        
        // 设置缓存大小
        let cache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
        
        NSURLCache.setSharedURLCache(cache)
    }
    

}

